package ru.isys.xml.util.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import ru.isys.xml.XmlItem;
import ru.isys.xml.template.DivInput;
import ru.isys.xml.template.FormInputFormOutput;

public class FormBuilder {
	String tmpfile = "request.xml";

	FormInputFormOutput formInputFormOutput;
	List<XmlItem> xmlItems;
	List<DivInput> divInputs;
	String xml;

	public FormBuilder(List<XmlItem> xmlItems) {
		this.xmlItems = xmlItems;
		this.divInputs = new ArrayList<DivInput>();
		this.xml = "";
	}

	public String build() {
		prepareFormDivs();
		xml += FormInputFormOutput.TAG;
		for (DivInput divInput : divInputs) {
			xml += divInput.getTag();
		}
		xml += FormInputFormOutput.CLOSE_TAG;
		return xml;
	}

	private void prepareFormDivs() {
		for (XmlItem item : xmlItems) {
			if (item.isModelField()) {
				if (item.getAttrs().isEmpty()) {
					divInputs.add(new DivInput(item.getFullName(), item
							.getLabel(), item.getAlert(), item.getHint()));
				} else {
					HashMap<String, String> attrs = item.getAttrs();
					for (Entry<String, String> set : attrs.entrySet()) {
						divInputs.add(new DivInput(item.getFullName() + "@"
								+ set.getKey(), item.getLabel(), item
								.getAlert(), item.getHint()));
					}
				}
			}
		}

	}
}
